<template>
  <view>
    <view class="category">
      <view class="category_title">
        <view class="title_inner">
          <uni-segmented-control
            :current="current"
            :values="items.map(v=>v.title)"
            @clickItem="onClickItem"
            style-type="text"
            active-color="#4cd964"
          ></uni-segmented-control>
        </view>
        <view class="iconfont iconsearch"></view>
      </view>

      <scroll-view
        enable-flex
        @scrolltolower="handleScrolltolower"
        scroll-y
        class="category_content"
      >
        <view class="cate_items" v-for="(item,index) in cateItems" :key="item.id">
          <go-detail :list="cateItems" :index="index">
            <image :src="item.thumb" mode="aspectFill" />
          </go-detail>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
import { uniSegmentedControl } from "@dcloudio/uni-ui";
import goDetail from "@/component/goDetail"
export default {
  data() {
    return {
      items: [
        { title: "最新", order: "new" },
        { title: "最热", order: "hot" }
      ],
      params: {
        limit: 30,
        skip: 0,
        order: "new"
      },
      current: 0,
      id: 0,
      cateItems: [],
      hasMore: true
    };
  },
  methods: {
    onClickItem(e) {
      if (this.current !== e.currentIndex) {
        this.current = e.currentIndex;
      }
      this.params.order = this.items[e.currentIndex].order;
      this.params.skip = 0;
      this.cateItems = [];
      this.getList();
    },
    getList() {
      this.request({
        url: `http://157.122.54.189:9088/image/v1/vertical/category/${this.id}/vertical`,
        data: this.params
      }).then(res => {
        if (res.res.vertical.length === 0) {
          this.hasMore = false;
          uni.showToast({
            title: "不要再滑辣!",
            icon: "none"
          });
          return;
        }
        
        this.cateItems = [...this.cateItems, ...res.res.vertical];
      });
    },
    handleScrolltolower() {
      if (this.hasMore) {
        this.params.skip += this.params.limit;
        this.getList();
      } else {
        uni.showToast({
          title: "没有啦!",
          icon: "none"
        });
      }
    }
  },
  onLoad(option) {
    this.id = option.id;
    this.getList();
  },
  components: {
    uniSegmentedControl,
    goDetail
  }
};
</script>

<style lang="scss">
.category {
  .category_title {
    position: relative;
    .title_inner {
      width: 60%;
      margin: 0 auto;
    }
    .iconsearch {
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
      right: 5%;
    }
  }
  .category_content {
    height: calc(100vh - 35px);

    display: flex;
    flex-wrap: wrap;
    .cate_items {
      width: 33.33%;
      border: 5rpx solid #fff;
    }
  }
}
</style>